import os
import time
from datetime import timedelta

import config
from SensitiveKeywordsChecker import SensitiveKeywordsChecker

# 使用os.path.join来构建路径，以提高可移植性
workspace_path = r'E:\workspace\old_docs'
# workspace_path = r'G:\00_transfer\TEMP'
input_path = os.path.join(workspace_path, 'input')
output_path = os.path.join(workspace_path, 'output')

log_file_path = os.path.join(workspace_path, 'file.log')
state_file_path = os.path.join(workspace_path, 'state.json')

# 初始化日志配置（确保只调用一次）
initial_logging_config = config.initial_logging_config
initial_logging_config()


# 创建一个函数来设置和写入日志文件
def setup_and_write_log(log_file_path, message):
    config.log_file_path = log_file_path

    os.makedirs(os.path.dirname(log_file_path), exist_ok=True)
    with open(log_file_path, 'w') as log_file:
        log_file.write(f"{message}\n")

    # 创建一个函数来记录和处理时间


def record_and_process_time(start_time):
    end_time = time.time()
    elapsed_time = end_time - start_time
    time_taken = timedelta(seconds=elapsed_time)
    logging_info = f"Total time taken: {time_taken}"
    config.print_console_and_write_log(logging_info)


def main():
    start_time = time.time()  # 记录开始时间
    setup_and_write_log(log_file_path, "Log started for the current session.")

    logging_info = f"Start Processing jpg files: {input_path}"
    config.print_console_and_write_log(logging_info)

    try:
        checker = SensitiveKeywordsChecker(input_path, output_path)
        checker.check_all_jpg_files()
    except Exception as e:
        config.print_console_and_write_log(f"An error occurred: {e}")

    record_and_process_time(start_time)


if __name__ == '__main__':
    main()
